package salesforce;

/**
 * 只能从两边拿牌，也就意味着剩下的牌都在中间
 * 拿的牌最大，也就是剩下的牌最小
 * 滑动窗口
 */
public class MaximumCardPoints {
    public int maxScore(int[] cardPoints, int k) {
        int window = cardPoints.length - k;
        int score = 0;
        int sum = 0;
        for (int i = 0; i < cardPoints.length; i++) {
            if (i < window) {
                score += cardPoints[i];
            }
            sum += cardPoints[i];
        }
        int tmp = score;
        for (int i = 0, j = window; j < cardPoints.length; i++, j++) {
            tmp = tmp + cardPoints[j] - cardPoints[i];
            score = Math.min(score, tmp);
        }
        return sum - score;
    }

    public static void main(String[] args) {
        int[] cardPoints = new int[]{1,2,3,4,5,6,1};
        System.out.println(new MaximumCardPoints().maxScore(cardPoints, 3));
    }
}
